# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.adminDao import AdminDao

from . import app

CORS(app, supports_credentials=True)
__author__ = "Prince_CHEN"


@app.route('/insertAdmin', methods=['POST'])
def insert_adminNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'roleId': request.json.get('roleId', 0),
        'roleType': request.json.get('roleType', 0),
        'adminName': request.json.get('adminName', ''),
        'adminPassword': request.json.get('adminPassword', ''),
        'adminSalt': request.json.get('adminSalt', ''),
        'adminDesc': request.json.get('adminDesc', ''),
        'adminRealName': request.json.get('adminRealName', ''),
        'adminTelephone': request.json.get('adminTelephone', ''),
        'adminEmail': request.json.get('adminEmail', ''),
        'adminIsLock': request.json.get('adminIsLock', 0)
    }

    info = AdminDao().insertAds(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findAdminById', methods=['POST'])
def find_admin_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    adminId = request.json.get('adminId', '')
    info = AdminDao().findAdminById(adminId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 1, "status": "error"}})


@app.route('/findAdminByCondition', methods=['POST'])
def find_admin_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = AdminDao().findAdminByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                  multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 0, "status": "No such data"}})


@app.route('/deleteAdminByIds', methods=['POST'])
def delete_admin_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = AdminDao().deleteAdmin(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateAdminById', methods=['POST'])
def update_admin_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        adminId = request.json['adminId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = AdminDao().selectAdmin(adminId)

    info = {
        'adminId': request.json['adminId'],
        'roleId': request.json.get('roleId', oldInfo[1]),
        'roleType': request.json.get('roleType', oldInfo[2]),
        'adminName': request.json.get('adminName', oldInfo[3]),
        'adminPassword': request.json.get('adminPassword', oldInfo[4]),
        'adminSalt': request.json.get('adminSalt', oldInfo[5]),
        'adminDesc': request.json.get('adminDesc', oldInfo[6]),
        'adminRealName': request.json.get('adminRealName', oldInfo[7]),
        'adminTelephone': request.json.get('adminTelephone', oldInfo[8]),
        'adminEmail': request.json.get('adminEmail', oldInfo[9]),
        'adminIsLock': request.json.get('adminIsLock', oldInfo[10])
    }

    info = AdminDao().updateAdmin(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/adminLogin', methods=['POST'])
def admin_loginNoToken():
    """
    login
    :return:
    """
    if not request.json:
        abort(400)

    loginInfo = {
        'admin_name': request.json.get('admin_name', ''),
        'admin_password': request.json.get('admin_password', '')
    }

    code, info = AdminDao().admin_login(loginInfo)
    if code == 1:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    elif code == 0:
        return jsonify({"message": {"code": 0, "status": "给用户已被禁用!"}})
    elif code == -1:
        return jsonify({"message": {"code": -1, "status": "用户不存在!"}})
    else:
        return jsonify({"message": {"code": -2, "status": "密码错误！"}})
